#include "Http.hpp"
#define WWWROOT "./http"
string RequestStr(const HttpRequest &req)
{
    stringstream s;
    s << req._method << " " << req._path << " " << req._version << "\r\n";
    for (auto &it : req._params)
        s << it.first << ": " << it.second << "\r\n";
    for (auto &it : req._headers)
        s << it.first << ": " << it.second << "\r\n";
    s << "\r\n"
      << req._body;
    return s.str();
}
void Hello(const HttpRequest &req, HttpResponse &rsp)
{
    rsp.SetContent(RequestStr(req), "text/plain");
}
void Login(const HttpRequest &req, HttpResponse &rsp)
{
    rsp.SetContent(RequestStr(req), "text/plain");
}
void Put(const HttpRequest &req, HttpResponse &rsp)
{
    std::string pathname = WWWROOT + req._path;
    Util::WriteFile(pathname, req._body);
}
void DelFile(const HttpRequest &req, HttpResponse &rsp)
{
    rsp.SetContent(RequestStr(req), "text/plain");
}
int main()
{
    HttpServer server(8088);
    server.SetBaseDir(WWWROOT);
    server.SetThreadCount(2);
    server.Get("/hello", Hello);
    server.Post("/login", Login);
    server.Put("/1234.txt", Put);
    server.Delete("/1234.txt", DelFile);
    server.Listen();
    return 0;
}